<?php  
session_start();  
include 'configs/config.php';  

$article_id = $_GET['id'] ?? null; // 获取文章 ID  
$message = "";  

// 确保文章 ID 存在  
if (!$article_id) {  
    header("Location: view_articles.php?message=文章ID缺失");  
    exit();  
}  

// 查询文章的详细信息  
$stmt = $conn->prepare("SELECT a.title, a.body, a.time, u.username FROM article a JOIN users u ON a.user_id = u.id WHERE a.id = ? AND a.visibility = '公开'");  
$stmt->bind_param("i", $article_id);  
$stmt->execute();  
$result = $stmt->get_result();  

if ($result->num_rows === 0) {  
    header("Location: view_articles.php?message=文章不存在或不可见");  
    exit();  
}  

$article = $result->fetch_assoc();  
$stmt->close();  
?>  

<!DOCTYPE html>  
<html lang="zh">  
<head>  
    <meta charset="UTF-8">  
    <title><?php echo htmlspecialchars($article['title']); ?></title>  
    <link rel="stylesheet" href="static/articles.css">  
</head>  

<body>  
    <h1><?php echo htmlspecialchars($article['title']); ?></h1>  
    <p>作者: <?php echo htmlspecialchars($article['username']); ?></p>  
    <p>发表于: <?php echo htmlspecialchars($article['time']); ?></p>  
    <div><?php echo nl2br(htmlspecialchars($article['body'])); ?></div>  

    <a href="view_articles.php" class="back-link">返回文章列表</a>  
</body>  
</html>